﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6._1
{
    public partial class Form1 : Form
    {

        private string mystr;
        private int flag;
        private string path;  //读取文件路径
        private string pathx;  //写入文件路径
        public Form1()
        {
            InitializeComponent();
            flag = 1;
            mystr = "";
            path = "";
            //检查该目录是否存在，不存在就重新生成一个
            pathx = Application.StartupPath+"\\data";         
            if (!Directory.Exists(pathx))
            {
                Directory.CreateDirectory(pathx);
            }
            pathx = pathx + "\\newfile.txt";
        }



        private void button1_Click(object sender, EventArgs e)
        {
            
            try {
                
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            
            openFileDialog1.InitialDirectory = Application.StartupPath;
            
            openFileDialog1.Filter = "text files (*.txt;)|*.txt;";
             
            openFileDialog1.RestoreDirectory = true;
                //是否成功选择文件
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    
                    path = openFileDialog1.FileName;
                    FileStream fs = File.OpenRead(path);
                    StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                    fs.Seek(0, SeekOrigin.Begin);
                    while (sr.Peek() > -1)
                    {
                        mystr = mystr + sr.ReadLine();
                    }
                    sr.Close();
                    richTextBox1.Text = mystr;

                    //第一个文件
                    if (flag == 1) {
                        MessageBox.Show(path + "   已读出此文件内容，请再选一次进行合并");
                        flag++;
                        this.button1.Text = "选择第二个文件";
                    }
                    //第二个文件
                    else
                    {
                        //写文件操作
                        if (File.Exists(pathx)) { File.Delete(pathx); }
                            FileStream wfs= File.OpenWrite(pathx);
                            StreamWriter sw = new StreamWriter(wfs, Encoding.UTF8);
                            sw.WriteLine(mystr);
                            sw.Close();
                        
                        MessageBox.Show(path + "   已进行合并");

                        //重新初始化操作
                        flag = 1;
                        this.button1.Text = "选择第一个文件";
                        mystr = "";
                    }
                }
                
            }
            catch (Exception)
            {
                //重新初始化操作
                flag = 1;
                this.button1.Text = "选择第一个文件";
                mystr = "";
            }
        }
    }
}
